package cc.customer.web.sys.controller.system;

import cc.customer.SocketIo.SocketIOService;
import cc.customer.common.domain.QueryRequest;
import cc.customer.common.domain.ResponseBo;
import cc.customer.common.utils.FileUtils;
import cc.customer.rabbit.domain.CconvRecord;
import cc.customer.system.domain.SocketOnline;
import cc.customer.system.domain.SysLog;
import cc.customer.system.service.LogService;
import cc.customer.web.sys.controller.base.BaseController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
public class LogController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private LogService logService;


    @RequestMapping("log")
    @PreAuthorize("hasAuthority('log:list')")
    public String index() {
        return "system/log/log_manage";
    }

    @RequestMapping("socket")
    public String socketIndex() {
        return "system/log/socket_log";
    }

    @RequestMapping("log/list")
    @PreAuthorize("hasAuthority('log:list')")
    @ResponseBody
    public Map<String, Object> logList(QueryRequest request, SysLog log) {
        return super.selectByPageNumSize(request, () -> this.logService.findAllLogs(log));
    }

    @RequestMapping("socket/list")
    @ResponseBody
    public Map<String, Object> SocketlogList(QueryRequest request, CconvRecord log) {
        return super.selectByPageNumSize(request, () -> this.logService.getAllSocketLogs(log));
    }
    /**获取socket当前连接成功的人**/
    @RequestMapping("socket/online")
    @ResponseBody
    public List<SocketOnline> getSocketOnline(){
        //Map<String, SocketIOClient> list =  socketIOService.getClientMap();
        List<SocketOnline> onlines = new ArrayList<>();
        /*list.forEach((key,val)->{
            SocketOnline online = new SocketOnline();
            online.setUserid(key);
            String attd  =val.getHandshakeData().getAddress().getAddress().getHostAddress();
            online.setAddress(attd);
            onlines.add(online);
        });*/
        return onlines;
    }
    @RequestMapping("log/excel")
    @ResponseBody
    public ResponseBo logExcel(SysLog log) {
        try {
            List<SysLog> list = this.logService.findAllLogs(log);
            return FileUtils.createExcelByPOIKit("系统日志表", list, SysLog.class);
        } catch (Exception e) {
            logger.error("导出系统日志Excel失败", e);
            return ResponseBo.error("导出Excel失败，请联系网站管理员！");
        }
    }

    @RequestMapping("log/delete")
    @ResponseBody
    public ResponseBo deleteLogss(String ids) {
        try {
            this.logService.deleteLogs(ids);
            return ResponseBo.ok("删除日志成功！");
        } catch (Exception e) {
            logger.error("删除日志失败", e);
            return ResponseBo.error("删除日志失败，请联系网站管理员！");
        }
    }
}
